<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="计划编号" prop="planNumber">
          <el-input
            v-model="queryParams.planNumber"
            placeholder="请输入计划编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input
            v-model="queryParams.planName"
            placeholder="请输入计划名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="计划日期" prop="params">
          <el-date-picker
            clearable
            v-model="queryParams.params"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截止日期" prop="params2">
          <el-date-picker
            clearable
            v-model="queryParams.params2"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            placeholder="请选择物料需求截止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="approvalStatus">
          <el-select v-model="queryParams.approvalStatus" placeholder="请选择状态" clearable>
            <el-option label="草稿" value="0"/>
            <el-option label="已审批" value="1"/>
            <el-option label="已完成" value="2"/>
            <el-option label="待审批" value="3"/>
            <el-option label="已驳回" value="4"/>
            <el-option label="终止" value="5"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="queryParams.remarks"
            placeholder="请输入备注"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['cg:plan:add']"
          >新增
          </el-button>
        </el-col>
        <!--             <el-col :span="1.5">
                      <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['cg:plan:edit']"
                      >修改
                      </el-button>
                    </el-col>
                    <el-col :span="1.5">
                      <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['cg:plan:remove']"
                      >删除
                      </el-button>
                    </el-col>-->
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['cg:plan:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
        <!--      <el-table-column type="selection" width="55" align="center"/>-->
        <el-table-column label="采购计划编号" align="center" prop="planNumber">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleView(scope.row)"
              v-hasPermi="['cg:plan:info']"
            >{{ scope.row.planNumber }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="采购计划名称" align="center" prop="planName"/>
        <el-table-column label="采购计划日期" align="center" prop="planDate" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="物料需求截止日期" align="center" prop="materialDeadline" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.materialDeadline, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remarks"/>
        <!--      <el-table-column label="付款类型" width="80" align="center" prop="paymentType">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.erp_payment_type" :value="scope.row.paymentType"/>
                </template>
              </el-table-column>
              <el-table-column label="预计成本" align="center" prop="estimatedCost">
                <template slot-scope="scope">
                  <el-tag type="danger">{{ formatCurrency(scope.row.estimatedCost) }}</el-tag>
                </template>
              </el-table-column>-->
        <el-table-column label="状态" align="center" prop="approvalStatus">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.approvalStatus === 0">草稿</el-tag>
            <el-tag type="primary" v-else-if="scope.row.approvalStatus === 1">已审批</el-tag>
            <el-tag type="success" v-else-if="scope.row.approvalStatus === 2">已完成</el-tag>
            <el-tag type="warning" v-else-if="scope.row.approvalStatus === 3">待审批</el-tag>
            <el-tag type="danger" v-else-if="scope.row.approvalStatus === 4">已驳回</el-tag>
            <el-tag type="danger" v-else-if="scope.row.approvalStatus === 5">终止</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="申请人" align="center" prop="applyPersonName"/>
        <el-table-column label="申请部门" align="center" prop="applyDepartment"/>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <!--          <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-view"
                        @click="handleView(scope.row)"
                      >查看
                      </el-button>-->

              <el-button
                size="mini"
                type="text"
                icon="el-icon-s-check"
                @click="handleView(scope.row)"
                v-show="scope.row.approvalStatus === 0 || scope.row.approvalStatus === 4"
                v-hasPermi="['cg:plan:check']"
              >审核
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-show="scope.row.approvalStatus === 0 || scope.row.approvalStatus === 4"
                v-hasPermi="['cg:plan:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-show="scope.row.approvalStatus === 0 || scope.row.approvalStatus === 4"
                v-hasPermi="['cg:plan:remove']"
              >删除
              </el-button>

              <el-button
                size="mini"
                type="text"
                icon="el-icon-switch-button"
                @click="handleTermination(scope.row)"
                v-show="scope.row.approvalStatus === 1"
                v-hasPermi="['cg:plan:termination']"
              >终止
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-video-play"
                @click="handleContinue(scope.row)"
                v-show="scope.row.approvalStatus === 5"
                v-hasPermi="['cg:plan:continue']"
              >继续执行
              </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改采购计划对话框 -->
    <add ref="add"></add>
    <!--查看/审核采购计划对话框 -->
    <infoView ref="infoView"></infoView>
    <!--      <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>-->
  </div>
</template>

<script>
import {listPlan, delPlan, terminationPlan, continuePlan} from "@/api/mes/cg/plan";

import Add from "@/views/mes/cg/plan/add.vue";
import InfoView from "@/views/mes/cg/plan/infoView.vue";


export default {
  name: "Plan",
  components: {
    Add,
    InfoView
  },
  dicts: ['erp_payment_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNumber:null,
        planName: null,
        planDate: null,
        materialDeadline: null,
        approvalStatus: null,
        params: [],
        params2: [],
        remarks:null
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatCurrency(number) {
      return new Intl.NumberFormat('zh-CN', {style: 'currency', currency: 'CNY'}).format(number);
    },
    /** 查询采购计划列表 */
    getList() {
      this.loading = true;
      const query = {...this.queryParams};
      if (query.params && query.params.length === 2) {
        query.params = {
          beginTime: query.params[0],
          endTime: query.params[1]
        };
      } else {
        query.params = null;
      }
      if (query.params2 && query.params2.length === 2) {
        query.params2 = {
          beginTime: query.params2[0],
          endTime: query.params2[1]
        };
      } else {
        query.params2 = null;
      }
      listPlan(query).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    /* cancel() {
       this.open = false;
       this.reset();
     },*/
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange: function (selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      let checked = selection.find(item => item.approvalStatus === 1)
      if (checked) {
        this.single = true
        this.multiple = true
        this.$modal.msgError('已审核的采购计划不能修改或删除')
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.add.open = true;
      this.$refs.add.autoGenFlag = false;
      this.$refs.add.title = '新增采购计划';
      this.$refs.add.optType = 'add';
      this.$refs.add.addGoodsList = [];
      this.$refs.add.init();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.add.open = true;
      this.$refs.add.autoGenFlag = false;
      this.$refs.add.title = '修改采购计划';
      this.$refs.add.optType = 'edit';
      this.$refs.add.update(row.planId);
    },
    handleView(row) {
      this.$refs.infoView.open = true;
      this.$refs.infoView.title = row.approvalStatus === 0 ? '审核采购计划' : '查看采购计划';
      this.$refs.infoView.init(row.planId);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal.confirm('是否确认删除采购计划编号为"' + row.planNumber + '"的数据项？').then(function () {
        return delPlan(planIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cg/plan/export', {
        ...this.queryParams
      }, `采购计划_${new Date().getTime()}.xlsx`)
    },
    // 终止按钮操作
    handleTermination(row) {
      this.$modal.confirm('是否确认终止编号为"' + row.planNumber + '"的采购计划？').then(function () {
        return terminationPlan(row.planId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("计划已终止");
      }).catch(() => {
      });
    },
    // 继续执行按钮操作
    handleContinue(row) {
      this.$modal.confirm('是否继续执行编号为"' + row.planNumber + '"的采购计划？').then(function () {
        return continuePlan(row.planId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("计划已继续执行");
      }).catch(() => {
      });
    },
  }
};
</script>
